﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMK.Tadong.OfficeToSwf
{
    /// <summary>
    /// 进程操作类
    /// </summary>
    public class ProcessManage
    {
        /// <summary>
        /// 杀死进程 
        /// 赵现发 20131126
        /// </summary>
        /// <param name="processName">进程名</param>
        /// <returns></returns>
        public static bool KillProcess(string processName)
        {
            #region

            string strProcessName = processName.Trim().ToUpper();
            System.Diagnostics.Process[] myPs;
            myPs = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in myPs)
            {
                if (p.Id != 0)
                {
                    try
                    {
                        if (p.Modules != null)
                            if (p.Modules.Count > 0)
                            {
                                System.Diagnostics.ProcessModule pm = p.Modules[0];
                                if (pm.ModuleName.Trim().ToUpper() == strProcessName)
                                {
                                    p.Kill();
                                    return true;
                                }
                            }
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return false;

            #endregion
        }
        /// <summary>
        /// 杀死进程 
        /// 赵现发 20131126
        /// </summary>
        /// <param name="lstProcessName">进程名</param>
        /// <returns></returns>
        public static bool KillProcess(List<string> lstProcessName)
        {
            #region
            try
            {
                for (int i = 0; i < lstProcessName.Count; i++)
                {
                    lstProcessName[i] = lstProcessName[i].Trim().ToUpper();
                }
                System.Diagnostics.Process[] myPs;
                myPs = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process p in myPs)
                {
                    if (p.Id != 0)
                    {
                        if (p.Modules != null)
                        {
                            if (p.Modules.Count > 0)
                            {
                                System.Diagnostics.ProcessModule pm = p.Modules[0];
                                string strPMName = pm.ModuleName.Trim().ToUpper();
                                foreach (string strName in lstProcessName)
                                {
                                    if (strPMName == strName)
                                    {
                                        p.Kill();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                return false;
            }
            return true;

            #endregion
        }

    }
}
